<?php
	class AdminDthink_Model_Area extends Zend_Db_Table_Abstract
	{
		protected $_name = 'area';
		protected $_primary = 'area_id';
		protected $_db;
		
		public function __construct()
		{
			$this->_db = Zend_Registry::get('db');
		}
		
		public function listArea()
		{
			$sql = $this->_db->select()
							 ->from($this->_name)
							 ->order('area_id DESC');
			$query = $this->_db->fetchAll($sql);
			return $query;
		}
		
		public function listVoucher()
		{
			$sql = $this->_db->select()
							->from($this->_name)
                            ->where('status = 1')
							->order('area_name ASC');
			$query = $this->_db->fetchAll($sql);
			return $query;
		}
		
		public function addArea($area_name)
		{
			$data = array(
				'area_name' => $area_name,
				'status' 	=> 0,
			);
			$result = $this->_db->insert($this->_name, $data);
			if($result)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
		public function getArea($area_id)
		{
			$sql = $this->_db->select()
							 ->from($this->_name)
							 ->where('area_id = ?', $area_id);
			$query = $this->_db->fetchRow($sql);
			return $query;
		}
        
        public function getAreaNameById($area_id)
        {
            $sql = $this->_db->select()
                            ->from($this->_name)
                            ->where('area_id = ?', $area_id);
            $result = $this->_db->fetchRow($sql);
            return $result['area_name'];
        }
		
		public function editArea($area_id, $area_name, $status)
		{
			$data = array(
				'area_name' => $area_name,
				'status'	=> $status,	
			);
			$where = 'area_id = ' . $area_id;
			$result = $this->_db->update($this->_name, $data, $where);
			if($result)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
		public function deleteArea($area_id)
		{
			$where = 'area_id = ' . $area_id;
			$result = $this->_db->delete($this->_name, $where);
			if($result)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
		public function deleteAreas($listId)
		{
			foreach($listId as $value)
			{
				$where = 'area_id = ' . $value;
				$sql = $this->_db->select()
								 ->from('product')
								 ->where($where);
				$row = $this->_db->fetchAll($sql);
				foreach ($row as $key)
				{
					if (is_file(PUBLIC_PATH . '/files/product/' . $key['product_image']))
					{
						unlink(PUBLIC_PATH . '/files/product/' . $key['product_image']);
					}
				}
				$this->_db->delete('product', $where);
				$this->_db->delete($this->_name, $where);
			}
		}
		
		public function checkArea($area_name, $area_id = '')
		{
            if($area_id != '')
            {
                $sql = $this->_db->select()
    							 ->from($this->_name)
    							 ->where('area_name = ? AND area_id = ?', $area_name, $area_id);
    			$result = $this->_db->fetchAll($sql);
    			return count($result);
            }
            else
            {
    			$sql = $this->_db->select()
    							 ->from($this->_name)
    							 ->where('area_name = ?', $area_name);
    			$result = $this->_db->fetchAll($sql);
    			return count($result);
            }
		}
		
		public function searchArea($keyword)
		{
			$where = "MATCH(area_name) AGAINST('" . $keyword . "' IN NATURAL LANGUAGE MODE)";
			$sql = $this->_db->select()
							 ->from($this->_name)
							 ->where($where);
			$result = $this->_db->fetchAll($sql);
			return $result;
		}
	}